using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Threading;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.Diagnostics;
using Microsoft.WindowsAzure.ServiceRuntime;
using Microsoft.WindowsAzure.StorageClient;
using Microsoft.WindowsAzure.Diagnostics.Management;

namespace PlatformWorkerRole
{
    public class WorkerRole : RoleEntryPoint
    {
        /*
        const string deploymentId = "8f743e9c9a344f378f9c7f7835a70df6";
        const string roleName = "PlatformWorkerRole";
        const string roleInstanceName = "PlatformWorkerRole_IN_0";
        */
        const string DIAGNOSTIC_STORAGE_CONN_STRING = "Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString";

        public override void Run()
        {
            // This is a sample worker implementation. Replace with your logic.
            Trace.WriteLine("$projectname$ entry point called", "Information");

            //PlatformTest.TableServiceTest();
            PlatformTest.BlobServiceTest();

            while (true)
            {
                Thread.Sleep(10000);
                Trace.WriteLine("Working", "Information");
            }
        }

        public override bool OnStart()
        {
            // Set the maximum number of concurrent connections 
            ServicePointManager.DefaultConnectionLimit = 12;

            // For information on handling configuration changes
            // see the MSDN topic at http://go.microsoft.com/fwlink/?LinkId=166357.

            CloudStorageAccount.SetConfigurationSettingPublisher(
                (configName, configSettingPublisher) =>
                {
                    string connStr = RoleEnvironment.GetConfigurationSettingValue(configName);
                    configSettingPublisher(connStr);
                }
            );

            DiagnosticMonitorConfiguration diagConfig = DiagnosticMonitor.GetDefaultInitialConfiguration();
            diagConfig.OverallQuotaInMB = 2048;
            diagConfig.ConfigurationChangePollInterval = TimeSpan.FromSeconds(30);
            diagConfig.Logs.BufferQuotaInMB = 100;
            diagConfig.Logs.ScheduledTransferLogLevelFilter = LogLevel.Verbose;
            diagConfig.Logs.ScheduledTransferPeriod = TimeSpan.FromSeconds(10);
            diagConfig.PerformanceCounters.DataSources.Add(
                new PerformanceCounterConfiguration()
                {
                    CounterSpecifier = @"\Processor(_Total)\% Processor Time",
                    SampleRate = TimeSpan.FromSeconds(5)
                }
            );
            diagConfig.PerformanceCounters.DataSources.Add(
                new PerformanceCounterConfiguration()
                {
                    CounterSpecifier = @"\Memory\Committed Bytes",
                    SampleRate = TimeSpan.FromSeconds(5)
                }
            );
            diagConfig.PerformanceCounters.BufferQuotaInMB = 100;
            diagConfig.PerformanceCounters.ScheduledTransferPeriod = TimeSpan.FromSeconds(5);

            DiagnosticMonitor.Start(DIAGNOSTIC_STORAGE_CONN_STRING, diagConfig);

            return base.OnStart();
        }
    }
}
